package cn.tedu.store.controller;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ExceptionHandler;

import cn.tedu.store.controller.ex.FileEmptyException;
import cn.tedu.store.controller.ex.FileSizeException;
import cn.tedu.store.controller.ex.FileStateException;
import cn.tedu.store.controller.ex.FileTypeException;
import cn.tedu.store.controller.ex.FileUploadException;
import cn.tedu.store.controller.ex.FileUploadIOException;
import cn.tedu.store.service.ex.AccessDeniedException;
import cn.tedu.store.service.ex.AddressLimitExceededException;
import cn.tedu.store.service.ex.AddressNotFoundException;
import cn.tedu.store.service.ex.CartNotFoundException;
import cn.tedu.store.service.ex.InsertException;
import cn.tedu.store.service.ex.PasswordErrorException;
import cn.tedu.store.service.ex.ServiceException;
import cn.tedu.store.service.ex.UpdateException;
import cn.tedu.store.service.ex.UserNameDuplicateException;
import cn.tedu.store.service.ex.UserNotFoundException;
import cn.tedu.store.util.ResponseResult;
/**
 * 控制器类的基类，处理所有的的我们定义的异常，这样子类不用一个一个try()catch{}
 * Spring要求处理异常的类是public
 * @author Administrator
 *
 */
public abstract class BaseController {
	
	// 表示响应成功，用户操作的是正确的，给子类用的
	protected static final Integer SUCCESS = 200;
	
	 /*从Session中获取用户uid*/
	//Integer.valueOf(String str) 返回的是Integer类型
	//Integer.parseInt(String str) 返回的是int类型	
	protected final Integer getUidFromSession(HttpSession session) {
		return  Integer.valueOf(session.getAttribute("uid").toString());
	}

	//从Session中获取用户名username
	protected final String getUsernameFromSession(HttpSession session) {
		return session.getAttribute("username").toString();
	}
	//出现异常，Spring框架自己调用，统一处理ServiceException类的异常
	@ExceptionHandler({ServiceException.class,FileUploadException.class})
	public ResponseResult<Void> handleException(Throwable e){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		//将业务层抛出来的异常信息，设置到JSON对象的message中
		rr.setMessage(e.getMessage());

		if(e instanceof UserNameDuplicateException) {
			//用户名已被占用
			rr.setState(4000);
		}else if(e instanceof UserNotFoundException) {
			rr.setState(4001);
		}else if(e instanceof PasswordErrorException) {
			rr.setState(4002);
		}else if(e instanceof InsertException) {
			rr.setState(5000);
		}else if(e instanceof UpdateException) {
			rr.setState(5001);
		}else if(e instanceof FileEmptyException) {
			rr.setState(6000);
		}else if(e instanceof FileSizeException) {
			rr.setState(6001);
		}else if(e instanceof FileTypeException) {
			rr.setState(6002);
		}else if(e instanceof FileStateException) {
			rr.setState(6003);
		}else if(e instanceof FileUploadIOException) {
			rr.setState(6004);
		}else if(e instanceof AddressLimitExceededException) {
			rr.setState(7001);
		}else if(e instanceof AccessDeniedException) {
			rr.setState(8001);
		}else if(e instanceof AddressNotFoundException) {
			rr.setState(9001);
		}else if(e instanceof CartNotFoundException) {
			rr.setState(9002);
		}
		
		return rr;
	}
}
